/**
 * 
 */
var WindMap = (function () {
    // 初始化

    function WindMap(options) { // 属性
        this.app = options.app;
        this.visible = false;
        this.map = options.map;
        this.dragStarted = false;
        this.windy = {};
        this.canvas = document.getElementById(options.canvas);//'windmap'
        this.init();
    }
    WindMap.prototype.init = function () {
        var that = this;

        this.map.on('moveend', function () {
            if (that.visible === true) {
                that.start();
            }
        });
        this.map.on("pointerdrag", function (ev) {
            if (!that.dragStarted) {
                $("#windmap").css("visibility", "hidden");
                that.dragStarted = true;
            }
        });
    };

    WindMap.prototype.loaddata = function () {
        fetch('/datas/data/gfs/current/current-wind-surface-level-gfs-0.5.epak').then(function (response) {
            return response.arrayBuffer();
        }).then(function (buffer) {
            //
            var packdata = decoder.decodeEpak(buffer);
            console.info(packdata);

            //Float32Array 
            //数据变换成从-180 到 180的经纬度区间
            var imgwidth = packdata.header.variables.lon.sequence.size;
            var imgheight = packdata.header.variables.lat.sequence.size;

            var wrapdata = new Float32Array(packdata.blocks[0].length);

            for (var irow = 0; irow < imgheight; irow++) {
                var sub1 = packdata.blocks[0].subarray(irow * imgwidth + imgwidth / 2, (irow + 1) * imgwidth);
                wrapdata.set(sub1, irow * imgwidth);

                var sub0 = packdata.blocks[0].subarray(irow * imgwidth, irow * imgwidth + imgwidth / 2);
                wrapdata.set(sub0, irow * imgwidth + imgwidth / 2);
            }
            packdata.blocks[0] = wrapdata;

            wrapdata = new Float32Array(packdata.blocks[1].length);

            for (var irow = 0; irow < imgheight; irow++) {
                var sub1 = packdata.blocks[1].subarray(irow * imgwidth + imgwidth / 2, (irow + 1) * imgwidth);
                wrapdata.set(sub1, irow * imgwidth);

                var sub0 = packdata.blocks[1].subarray(irow * imgwidth, irow * imgwidth + imgwidth / 2);
                wrapdata.set(sub0, irow * imgwidth + imgwidth / 2);
            }
            packdata.blocks[1] = wrapdata;

            //由于下载处理的数据和风向图数据的格式差异太大，
            //修改数据源或者风向图的算法都不太可能，为了正常显示
            //中间做一个桥接，就是把原始的数据读取出来，按照目标格式重新填写。
            var v = [], u = [], t = []; //目标格式的uv分量和t温度分量，都是一度一个数据的，大小是360*181=65160，而下载的数据大小是 720*361=259920.正好是1/4的数据，隔一个取一个最好
            for (var i = 0; i < 65160; i++) {
                v.push(packdata.blocks[0][i * 2]);
            }
            for (var i = 0; i < 65160; i++) {
                u.push(packdata.blocks[1][i * 2]);
                t.push(30);//温度设定为0
            }
            winddata[0].data = t;
            winddata[1].data = v;
            winddata[2].data = u;
            that.windy = new Windy({
                canvas: that.canvas,
                data: winddata
            });
            that.start();
        });
    }

    WindMap.prototype.stop = function () {
        this.visible = false;
        if (this.windy) {
            this.windy.stop();
            $("#windmap").css("visibility", "hidden");
        }
        /*
        this.map.interactions=ol.interaction.defaults({
            altShiftDragRotate: false,
            rotate: false
        });*/
    }
    WindMap.prototype.start = function () {
        var that = this;
        if (this.app.view !== "2d") {
            return;
        }
        this.visible = true;
        if (that.windy) {
            that.dragStarted = false;
            $("#windmap").css("visibility", "visible");
            if (!that.visible || !that.windy) {
                return;
            }
            //that.windy.stop();
            var mapSize = that.map.getSize();
            var extent = that.map.getView().calculateExtent(mapSize);
            extent = ol.proj.transformExtent(extent, 'EPSG:3857', 'EPSG:4326');

            that.canvas.width = mapSize[0];
            that.canvas.height = mapSize[1];
            console.info(extent);
            that.windy.start([[0, 0], [that.canvas.width, that.canvas.height]],
                that.canvas.width, that.canvas.height, [[extent[0], extent[1]],
                [extent[2], extent[3]]]);
        }
        /*
        this.map.interactions=ol.interaction.defaults({
            altShiftDragRotate: false,
            rotate: false
        });*/
    }
    //
    // 初始化


    // 返回
    return WindMap;
})();

export default {
    WindMap
}